import { tableBaseApi } from '../../../assets/services/api-common.js';
import Edit from '../components/AddEdit/Edit.js';
const genderOptions = [
	{ text: '男', value: 1 },
	{ text: '女', value: 2 },
];
export default function(midder) {
	return [{ text: 'ID', prop: 'id' }, {
			text: '姓名',
			prop: 'name',
			renderItem(type, prop, text) {
				return $Input({ $name: prop, $label: text });
			}
		},
		{
			text: '性别',
			prop: 'gender',
			renderItem(type, prop, text) {
				return $Select({
					$name: prop,
					$label: text,
					$options: genderOptions
				})
			},
			// element：data.row
			// antd：record为行数据
			render(data, record) {
				const { gender } = env === 'react-antd' ? record : data.row;
				if (gender) genderOptions.find(opt => opt.value === gender).text;
			}
		},
		{
			text: '年龄',
			prop: 'age',
			renderItem(type, prop, text) {
				return $InputNumber({ $name: prop, $label: text });
			},
		},
		{
			prop: 'action',
			width: '220px',
			fixed: 'right',
			text: '操作',
			// antd中需要配置key
			key: 'action',
			// element：data.row
			// antd：record为行数据
			render: (data, record) => {
				const initNode = row => this.render({
					is: '$ActionButtons',
					buttons: [{
						text: '详情',
						type: 'success',
						tip: '查看本条数据',
						onClick: async () => {
							const data = await tableBaseApi.detail({ id: row.id });
							console.log('本条数据的完整信息：', data);
						}
					}, {
						text: '编辑',
						type: 'primary',
						tip: '修改本条数据',
						onClick: async () => {
							midder.open(Edit, tableBaseApi, 'edit', {
								record: row,
								param: { class: 'no-padding' }
							});
						}
					}, {
						text: '删除',
						type: 'danger',
						pop: '确定删除？',
						onClick: async () => {
							await tableBaseApi.delete({ id: row.id });
							this.$msg('操作成功');
							midder.table.reload();
						}
					}],
				});
				if (env === 'react-antd') {
					// antd-design需要转Node节点
					return initNode(record).getNative();
				} else {
					return initNode(data.row);
				}
			}
		},
	]
}